package com.dib.neo.bkfs.fs;

import java.io.IOException;
import java.nio.channels.AsynchronousFileChannel;
import java.nio.channels.Channel;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import org.apache.curator.framework.recipes.locks.InterProcessLock;
import org.apache.curator.framework.recipes.locks.InterProcessReadWriteLock;

public class BKFileLock extends FileLock {
  private volatile boolean valid = true;
  private final InterProcessLock lock;

  protected BKFileLock(InterProcessLock lock,FileChannel channel, long position, long size,
      boolean shared) {
    super(channel, position, size, shared);
    this.lock = lock;
  }


  public boolean isValid() {
    return valid;
  }

  void invalidate() {
    assert Thread.holdsLock(this);
    valid = false;
  }

  public synchronized void release() throws IOException {
    try {
      lock.release();
    } catch (Exception e) {
      //throw new IOException(e);
    }
  }

}
